package com.lc.zpyh.ui.activity.home;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.request.SelectSchoolByProvincesApi;
import com.lc.zpyh.http.response.SelectSchoolByProvincesBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.util.constant.EvenConstant;
import com.lc.zpyh.util.eventbus.Event;
import com.lc.zpyh.util.eventbus.EventBusUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressSearchActivity extends AppActivity {
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.title_bar_txt_right)
    TextView titleBarTxtRight;
    @BindView(R.id.tv_school)
    TextView tvSchool;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.rv_address_lst)
    RecyclerView rvAddressLst;
    @BindView(R.id.ll_adressnow)
    LinearLayout llAdressnow;
    private BaseQuickAdapter<SelectSchoolByProvincesBean.SchoolsBean, BaseViewHolder> adapter;
    List<SelectSchoolByProvincesBean.SchoolsBean> dataBeanList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_addresssearch;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        tvSchool.setText(SPUtil.get(this, IntentKey.ADDRESS, ""));
        if (SPUtil.get(this, IntentKey.ADDRESS, "")!=null){
            llAdressnow.setVisibility(View.VISIBLE);
        }

        selectSchoolByProvincesName("");
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(etSearch.getText().toString().trim())) {
                    selectSchoolByProvincesName(etSearch.getText().toString().trim());

                }
            }
        });


    }

    private void selectSchoolByProvincesName(String schoolName) {
        EasyHttp.post(this)
                .api(new SelectSchoolByProvincesApi().setSchoolName(schoolName)).request(new HttpCallback<SelectSchoolByProvincesBean>(this) {

            @Override
            public void onSucceed(SelectSchoolByProvincesBean data) {
                if (data.getCode() == 0) {
                    dataBeanList.clear();
                    dataBeanList.addAll(data.getSchools());
                    if (adapter == null) {
                        adapter = new BaseQuickAdapter<SelectSchoolByProvincesBean.SchoolsBean, BaseViewHolder>
                                (R.layout.item_address_search, dataBeanList) {

                            @Override
                            protected void convert(BaseViewHolder baseViewHolder, SelectSchoolByProvincesBean.SchoolsBean dataBean) {
                                baseViewHolder.setText(R.id.tv_school, dataBean.getSchoolName());


                            }

                        };
                        rvAddressLst.setAdapter(adapter);
                    } else {
                        adapter.notifyDataSetChanged();
                    }

                    adapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {

                            EventBusUtils.sendEvent(new Event(EvenConstant.QUWANSHANXIN, dataBeanList.get(position).getSchoolName()));
                            EventBusUtils.sendEvent(new Event(EvenConstant.QUWANSHANXINID, dataBeanList.get(position).getSchoolId()));
                            SPUtil.put(AddressSearchActivity.this, IntentKey.ADDRESS, dataBeanList.get(position).getSchoolName());
                            SPUtil.put(AddressSearchActivity.this, IntentKey.ADDRESSID, dataBeanList.get(position).getSchoolId());
                            finish();
                        }
                    });


                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });

    }

    @OnClick(R.id.title_bar_txt_right)
    public void onClick() {
        selectSchoolByProvincesName(etSearch.getText().toString().trim());
    }
}
